import request from '@/utils/request'

/**
 * 联系人相关API接口
 */
export const contactsApi = {
  // ==================== 好友关系管理 ====================

  /**
   * 获取好友列表
   */
  getFriendsList(group?: string, keyword?: string) {
    return request.get('/contacts', {
      params: { group, keyword }
    })
  },

  /**
   * 获取好友列表（分页）
   */
  getFriendsPage(group?: string, keyword?: string, page = 0, size = 20) {
    return request.get('/contacts/page', {
      params: { group, keyword, page, size }
    })
  },

  /**
   * 获取在线好友
   */
  getOnlineFriends() {
    return request.get('/contacts/online')
  },

  /**
   * 更新好友信息
   */
  updateFriend(relationshipId: number, data: { groupName?: string, remark?: string }) {
    return request.put(`/contacts/${relationshipId}`, data)
  },

  /**
   * 删除好友
   */
  deleteFriend(relationshipId: number) {
    return request.delete(`/contacts/${relationshipId}`)
  },

  /**
   * 获取好友分组
   */
  getFriendGroups() {
    return request.get('/contacts/groups')
  },

  /**
   * 获取好友分组详细信息
   */
  getFriendGroupsDetail() {
    return request.get('/contacts/groups/detail')
  },

  /**
   * 创建好友分组
   */
  createFriendGroup(data: { groupName: string, description?: string, sortOrder?: number }) {
    return request.post('/contacts/groups', data)
  },

  /**
   * 删除好友分组
   */
  deleteFriendGroup(groupId: number) {
    return request.delete(`/contacts/groups/${groupId}`)
  },

  // ==================== 好友申请管理 ====================

  /**
   * 发送好友申请
   */
  sendFriendRequest(data: { toUserId: number, message?: string }) {
    return request.post('/contacts/requests', data)
  },

  /**
   * 处理好友申请
   */
  handleFriendRequest(requestId: number, data: { action: 'ACCEPT' | 'REJECT', responseMessage?: string }) {
    return request.put(`/contacts/requests/${requestId}`, data)
  },

  /**
   * 获取收到的好友申请
   */
  getReceivedRequests(status?: string, page = 0, size = 10) {
    return request.get('/contacts/requests/received', {
      params: { status, page, size }
    })
  },

  /**
   * 获取发送的好友申请
   */
  getSentRequests(status?: string, page = 0, size = 10) {
    return request.get('/contacts/requests/sent', {
      params: { status, page, size }
    })
  },

  /**
   * 获取待处理申请数量
   */
  getPendingRequestsCount() {
    return request.get('/contacts/requests/pending-count')
  },

  // ==================== 用户搜索 ====================

  /**
   * 搜索用户
   */
  searchUsers(data: {
    keyword: string
    searchType?: 'USERNAME' | 'EMAIL' | 'REAL_NAME' | 'ALL'
    excludeFriends?: boolean
    page?: number
    size?: number
  }) {
    return request.post('/contacts/search', data)
  },

  /**
   * 根据用户名查找用户
   */
  findUserByUsername(username: string) {
    return request.get(`/contacts/search/username/${username}`)
  },

  // ==================== 隐私设置管理 ====================

  /**
   * 获取隐私设置
   */
  getPrivacySettings() {
    return request.get('/contacts/privacy')
  },

  /**
   * 更新隐私设置
   */
  updatePrivacySettings(data: {
    allowSearchByUsername: boolean
    allowSearchByEmail: boolean
    allowSearchByRealName: boolean
    showOnlineStatus: boolean
    allowFriendRequests: boolean
    autoAcceptTeamMembers: boolean
  }) {
    return request.put('/contacts/privacy', data)
  },

  // ==================== 统计信息 ====================

  /**
   * 获取联系人统计
   */
  getContactsStatistics() {
    return request.get('/contacts/statistics')
  }
}

// 导出类型定义
export interface FriendInfo {
  id: number
  userId: number
  username: string
  realName: string
  avatarUrl?: string
  userType: 'BASIC' | 'VIP' | 'ADMIN'
  groupName: string
  remark?: string
  onlineStatus: 'ONLINE' | 'OFFLINE' | 'AWAY' | 'BUSY'
  lastSeen: string
  friendSince: string
  relationshipId: number
}

export interface FriendRequest {
  id: number
  fromUser: {
    id: number
    username: string
    realName: string
    avatarUrl?: string
    userType: 'BASIC' | 'VIP' | 'ADMIN'
  }
  toUser: {
    id: number
    username: string
    realName: string
    avatarUrl?: string
    userType: 'BASIC' | 'VIP' | 'ADMIN'
  }
  status: 'PENDING' | 'ACCEPTED' | 'REJECTED' | 'EXPIRED'
  message?: string
  responseMessage?: string
  createdAt: string
  updatedAt: string
  expiresAt: string
}

export interface UserSearchResult {
  id: number
  username: string
  realName: string
  avatarUrl?: string
  userType: 'BASIC' | 'VIP' | 'ADMIN'
  isFriend: boolean
  hasPendingRequest: boolean
  canSendRequest: boolean
  createdAt: string
}

export interface PrivacySettings {
  id: number
  userId: number
  allowSearchByUsername: boolean
  allowSearchByEmail: boolean
  allowSearchByRealName: boolean
  showOnlineStatus: boolean
  allowFriendRequests: boolean
  autoAcceptTeamMembers: boolean
  createdAt: string
  updatedAt: string
}

export interface ContactsStatistics {
  totalFriends: number
  onlineFriends: number
  pendingRequests: number
  sentRequests: number
}
